import { UserLoginApi, phoneLoginApi, userReqisterApi, getIndexListApi, getCategoryLeftApi, getCategoryRightApi, postCategoryListAPi, postBrandListApi } from "@/api/index"
const actions = {
    //通过提交mutations中的方法，达到修改状态的目的，通常异步请求在这里
    async actChangeIsLogin(context, prams) {
        //进行ajax请求
        let result = await UserLoginApi(prams.user_name, prams.user_password)
        if (result.status == 200) {
            localStorage.setItem('token', JSON.stringify(result.token))
            context.commit("changeIsLogin", result.token)
        }
    },
    async actPhoneLogin(context, params) {
        let result = await phoneLoginApi(params)
        console.log(result);
        if (result.status == 200) {
            localStorage.setItem('token', JSON.stringify(result.token))
            context.commit("changeIsLogin", result.token)
        }
    },
    async actReqister(context, params) {
        console.log(result);
        let result = await userReqisterApi(params)
        if (result.status == 200) {
            localStorage.setItem('token', JSON.stringify(result.token))
            context.commit("changeIsLogin", result.token)
        }
    },
    // 首页列表
    async actChangeListDatas(context, params) {
        context.commit("changeIsLoading", true)
        let result = await getIndexListApi(params)
        if (result.status == "success") {
            context.commit("changeListDatas", result.data)
        }
    },
    // 分类左侧数据
    async actChangeCategoryLeftDatas(context) {

        let result = await getCategoryLeftApi()

        context.commit("changeCategoryLeftDatas", result.data)
    },
    // 分类右侧数据
    async actChangeCategoryRightDatas(context, params) {
        context.commit("changeIsLoading", true)
        let result = await getCategoryRightApi(params)
        if (result.status == "success") {
            context.commit("changeIsLoading", false)
        }
        context.commit("changeCategoryRightDatas", result.data)
    },
    //分类内页数据
    async actChangeCategoryListDatas(context, params) {
        context.commit("changeIsLoading", true)
        let result = await postCategoryListAPi(params)
        console.log(result);
        if (result.data.length != 0) {
            context.commit("changeCategoryListDatas", result.data)
            context.commit("changeIsLoading", false)
        } else {
            context.commit("changeIsLoading", false)

        }
    },
    // 品牌列表
    async actChangeBrandListDatas(context, params) {
        let result = await postBrandListApi(params)
        console.log(result);
        context.commit("changeBrandListDatas", result.data)
    }
}


export default actions